package graduation.claf.controller;

import graduation.claf.pojo.User;
import graduation.claf.service.ifs.IUserService;
import graduation.claf.tools.Constant;
import graduation.claf.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/portal")
public class UserController {

    @Autowired
    IUserService userService;

    @RequestMapping(value = "/user/login.do")
    public ServerResponse Login(@RequestBody User user, HttpSession session){
        String username = user.getUsername();
        String password = user.getPassword();
        ServerResponse serverResponse = userService.loginLogic(username,password);
        if (serverResponse.isSuccess()){
            session.setAttribute(Constant.CURRENT_USER,serverResponse.getData());
        }
        return serverResponse;
    }

    @RequestMapping(value = "/user/register.do")
    public ServerResponse Register(@RequestBody User user){
        return userService.registerLogic(user);
    }

    @RequestMapping(value = "/user/BookInfo.do")
    public ServerResponse GetBookInfo(String bookId){
        return userService.getBookInfo(bookId);
    }

//    @PostMapping(value = "testJson.do")
//    public ServerResponse TestJson(@RequestBody JSONObject jsonObject){
//        System.out.println(jsonObject.toJSONString());
//        return ServerResponse.createServerResponseBySuccess();
//    }

    @PostMapping(value = "testJson.do")
    public ServerResponse TestJson(@RequestBody User user){
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        return ServerResponse.createServerResponseBySuccess();
    }
}
